
var a = 1;
var b = 2;
var c = 3;

//console.log(HPI.inArray({a:1, b : 1}, [{b : 1, a : 11}, {c : 1, e : 2}]));
/*
var _b = [
			{'field1' : 1, 'field2' : 'b'}
		   ,{'field1' : 1, 'field2' : 'c'}
		   ,{'field1' : 2, 'field2' : 'a'}
		]
	,c   = {'field1' : 'asc', 'field2' : 'desc'}

	,_s  = []
	,_m = _b
	,cnt = 0
	,crd = HPI.count(_b) - 1
	,_sk = [];

//console.log(crd);

//console.log(HPI.sort(_b, 0, crd, 'field2', 'desc'))

*/



//console.log(x);
/*
for(var k in c ){

	var t = c[k];
	
	_s  = HPI.sort(_b, 0, crd, k, t);
	
	if( cnt == 0 ){
		_m = HPI.mergeSorted(_s, _m);
		//console.log(_m);
	}
	else{
		//console.log(HPI.keys(_m, _sk));
		//console.log(_s);
		_m = HPI.mergeSorted(HPI.keys(_m, _sk), _s);
		
	}
			   
	cnt++;
	
	_sk = _sk.concat([k]);

}
*/
//console.log(_m);
/*
foreach($c as $k=>$t){
	$_s  = _sort($_b, 0, $crd, $k, $t);
	$_m = $cnt == 0
			   ? _merge_sorted($_s, $_m)
			   : _merge_sorted(_keys($_m, $_sk), $_s);
	$cnt++;
	$_sk[] = $k;
}
*/


//print_r($_m);


if( false && typeof openDatabase == 'function' ){
	var shortName = 'crcInv1';
	var version = '1.0';
	var displayName = 'CRC Database';
	var maxSize = 1024 * 1024 * 5; // in bytes
	var db = openDatabase(shortName, version, displayName, maxSize);

	function ExecuteReaderWithSingleCallback(query, db, callback){

		callback = callback || function(t, r){}

		db.transaction(
			function (transaction) {
				//console.log('a');
				console.log(' ---- ');
				console.log(query);
				//console.log(this);
				transaction.executeSql(
					query
					,[]
					,function(transaction, result){
						//console.log(transaction);
						//console.log(result);
						//console.log(this);
						callback(transaction, result);
					}
					,function (transaction, error) {
						//console.log(transaction);
						//console.log(error);
						//console.log(this);
						callback(transaction, error);
					}
				);
			}
		);

	}

	function printRows(r){
	
		if( r.rows == undefined ) {console.log("No result. " + r.message); return; }
		
		for(var i = 0, len = r.rows.length; i < len; i++){
			var row = r.rows.item(i)
			    ,_r   = "Row: " + ((i + 1).toString())
				,fI = 0;
			for (var field in row ){
				_r = _r + (fI == 0 ? "" : "," ) + " [" + field + "] : " + row[field];
				fI++;
			}
			console.log( _r );
		}
		
	}
	
	/*
	console.log(db);

	query = "DROP TABLE LoginSignature;";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	query = "DROP TABLE IF EXISTS LoginSignature;";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	query = "Create Table LoginSignature (_id integer not null primary key autoincrement, LoginID text, StoreID text,LoginTime text, Signature text)";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	query = "INSERT INTO LoginSignature (_id, LoginID, StoreID,LoginTime, Signature) VALUES (null, 1, 1, '12:00:00', 'test')";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	query = "INSERT INTO LoginSignature (_id, LoginID, StoreID,LoginTime, Signature) VALUES (null, 1, 1, '12:00:00', 'test')";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	//note: error
	query = "INSERTs INTO LoginSignature (_id, LoginID, StoreID,LoginTime, Signature) VALUES (null, 1, 1, '12:00:00', 'test')";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	query = "Select _id, LoginID, StoreID, LoginTime, Signature From LoginSignature limit 1";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	query = "Select _id, LoginID, StoreID, LoginTime, Signature From LoginSignature;";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	//note: error
	query = "sSelect _id, LoginID, StoreID, LoginTime, Signature From LoginSignature limit 1";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	*/

	query = "DROP TABLE a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	
	query = "DROP TABLE IF EXISTS a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	
	query = "CREATE TABLE a (field1 int primary key, field2 varchar(5))";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	query = "INSERT INTO a VALUES(5, 'a')";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});	
	
	query = "INSERT INTO a VALUES(6, 'b')";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	// query = "INSERT INTO a SELECT 1, 'a' UNION SELECT 2, 'b'";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});	
	
	// query = "INSERT INTO a (field2) VALUES ('c')";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});	
	
	// query = "INSERT INTO a (field2) VALUES ('d')";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	// query = "INSERT INTO a (field3) VALUES ('d')";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	// query = 'INSERT INTO a (field2) VALUES ("a\'a")';
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	// query = "INSERT INTO non_existing (field3) VALUES ('d')";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	query = "SELECT * FROM A";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){
		printRows(r);
	});
	
	// query = "UPDATE a SET field2 = 'e'";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	// query = "SELECT * FROM non_existing";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	

	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	//query = "DELETE FROM a";
	//ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	// query = "SELECT * FROM a LIMIT 2,1";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){
		// printRows(r);
	// });
	
	/*
	query = "SELECT * FROM (SELECT 'a' AS field1, 'b' AS field1)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	query = "SELECT [field1:1] FROM (SELECT 'a' AS field1, 'b' AS field1)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	query = "SELECT field1 FROM (SELECT 'a' AS field1)x, (SELECT 'a' AS field1)y";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	query = "SELECT field1 as 'a',field2 as 'a' FROM a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
	
	query = "SELECT 'a' as 'field', 'a' as 'field' FROM a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	*/
	
	var query = "DROP TABLE IF EXISTS b";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	var query = "CREATE TABLE b(field1 int)";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	var query = "INSERT INTO b VALUES(7)";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});

	// var query = "SELECT * FROM(SELECT 3 as field1, field1 FROM(SELECT  4 as field1, a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT 0 as field1, * FROM(SELECT 1 as field1, * FROM(SELECT 2 as field1, * FROM(SELECT 3 as field1, * FROM(SELECT  4 as field1, a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x)x)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT * FROM(SELECT * FROM(SELECT * FROM(SELECT 3 as field1, * FROM(SELECT  4 as field1, a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x)x)x"
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	var query = "SELECT * FROM (SELECT 3 as field1, * FROM(SELECT  4 as field1, a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x"
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	var query = "SELECT 0 as field1, * FROM (SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x"
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	var query = "SELECT * FROM(SELECT  4 as field1, a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x"
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT 0 as field1, * FROM ( SELECT 1 as field1, * FROM (SELECT 2 as field1 FROM ( SELECT 3 as field1, [field1:2], * FROM(SELECT  4 as field1, a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x)x)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT 'a' as field1, * FROM b INNER JOIN a ON a.field1 < b.field1";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT 'b' as field1, * FROM(SELECT *, 'a' as field1 FROM b INNER JOIN a ON a.field1 < b.field1)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT * FROM(SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	// var query = "SELECT  'x' as field1, 'y' as field1, 'z' as field1, * FROM(SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	// var query = "SELECT * FROM(SELECT  'x' as field1, 'y' as field1,  'z' as field1, 'a' as field1, * FROM(SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	// var query = "SELECT 'a' as field1, * FROM(SELECT  'x' as field1, * FROM(SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	// var query = "SELECT * FROM(SELECT 'a' as field1, 'b' as field1, 'c' as 'field1', * FROM(SELECT  'x' as field1, 'y' as field1,  'z' as field1, * FROM(SELECT a.field1, b.field1, 10 as field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x)x";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	// var query = "SELECT * FROM(SELECT field1, 'x' as field1 FROM a)x INNER JOIN (SELECT field1, 'g' as field1 FROM b)y ON x.field1 < y.field1";
	// ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	var query = "SELECT * FROM (SELECT * FROM(SELECT field1, 'x' as field1, 'a' as field1 FROM a)x INNER JOIN (SELECT field1, 'g' as field1 FROM b)y ON x.field1 < y.field1)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	var query = "SELECT * FROM(SELECT field1, 'x' as field1 FROM a)x INNER JOIN (SELECT field1, 'g' as field1 FROM b)y ON x.field1 < y.field1";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	var query = "SELECT * FROM(SELECT * FROM a INNER JOIN b ON a.field1 < b.field1)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	
	/*
	var query = "SELECT 'z' as field1, * FROM(SELECT  'x' as field1, 'y' as field1,  'z' as field1, * FROM(SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	var query = "SELECT * FROM(SELECT 'z' as field1, * FROM(SELECT  'x' as field1, 'y' as field1,  'z' as field1, * FROM(SELECT a.field1, b.field1 FROM b INNER JOIN a ON a.field1 < b.field1)x)x)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	*/
	
	{
	/*
	query = "SELECT b.field1, a.field1 FROM a, b ";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){		
		printRows(r);
	});
	
	query = "SELECT a.field1, b.field1 FROM a, b";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	query = "SELECT a.field1, b.field1 FROM a INNER JOIN b ON a.field1 < b.field1";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	query = "DROP TABLE IF EXISTS c";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	query = "CREATE TABLE c ([field .1] int)";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});
	
	query = "INSERT INTO c VALUES(100)";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){});	

	query = "SELECT * FROM c";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	

	query = "SELECT a.field1, a.field2 FROM a _a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	query = "SELECT * FROM (SELECT * FROM (SELECT 'a' as 'field1')x)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	//causes ambiguous field "a.field1" (and it would have been the case if we use other types of JOINs)
	query = "SELECT * FROM a,a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	

	//causes ambiguous field "a.field1" (and it would have been the case if we use other types of JOINs)
	query = "SELECT * FROM a, (SELECT 'value' as 'field1')a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	//ambiguous field1
	query = "SELECT b.field1, a.field1 FROM a,b WHERE field1 = 6";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	

	//causes ambiguous field "a.field1" (and it would have been the case if we use other types of JOINs)
	query = "SELECT field1 FROM a, (SELECT 'value' as 'field1')a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	//causes ambiguous field "a.field1" (and it would have been the case if we use other types of JOINs)
	query = "SELECT * FROM a, b A";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	//causes ambiguous field "a.field1" (and it would have been the case if we use other types of JOINs)
	query = "SELECT field1 FROM a, b";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	//does not cause duplicate correlation name
	query = "SELECT * FROM (SELECT * FROM(SELECT 'value' as 'field3')x)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	//does not cause ambiguous field (incorrect cartesian product):
	//only two fields of "a" (field1 and field2), and each row in "a" is duplicated twice.
	query = "SELECT * FROM a,a _a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});
	
	//two rows: field1 and field2 are the values of "a" since it "a" was first specified; if "b" was first
	//specified, "field1" would have value 7.
	query = "SELECT * FROM a,b";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});

	//only 1 column, field1. field1 takes the value from b, since b.field1 was first specified in the list
	query = "SELECT b.field1, a.field1 FROM a,b";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
	
	//only 1 column, field1. field1 takes the value from b, since b.field1 was first specified in the list
	query = "SELECT a.field1, b.field1 FROM a,b";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
		
	//only 1 column, field1. field1 takes the value from b, since b.field1 was first specified in the list
	query = "SELECT a.field1+1 as field1, a.field1 FROM a WHERE field1 = 6";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
	
	query = "SELECT *,*, 'c' as 'c' FROM a";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
	
	query = "SELECT * FROM (SELECT * FROM (SELECT 1 AS 'a', 2 AS 'b')tbl1 INNER JOIN (SELECT 1 AS 'a', 3 as 'c')tbl2 ON tbl1.a = tbl2.a)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	

	query = "SELECT * FROM (SELECT * FROM a INNER JOIN b ON a.field1 = b.field1)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
	
	query = "SELECT * FROM b INNER JOIN a ON a.field1 < b.field1";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
	
	query = "SELECT * FROM (SELECT * FROM a INNER JOIN b ON a.field1 < b.field1)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});		
	
	query = "SELECT  * FROM(SELECT * FROM (SELECT 1+1 AS 'field1', * FROM a INNER JOIN b ON a.field1 < b.field1)x)x";
	ExecuteReaderWithSingleCallback(query, db, function(t, r){printRows(r);});	
	
	*/
	}
	
}


/*
//------------------------------------------------
// This will improve our code to be more readable and shorter
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
// Now we can open our database
const dbName = "MyTestDatabase";

// This is what our customer data looks like.
const customerData = [
  { ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" },
  { ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" }
];

 
var request = indexedDB.open(dbName, 4);
 
request.onerror = function(event) {
  // Handle errors.
};
request.onupgradeneeded = function(event) {
  var db = event.target.result;
 
  // Create an objectStore to hold information about our customers. We're
  // going to use "ssn" as our key path because it's guaranteed to be
  // unique.
  var objectStore = db.createObjectStore("customers", { keyPath: "ssn" });
 
  // Create an index to search customers by name. We may have duplicates
  // so we can't use a unique index.
  objectStore.createIndex("name", "name", { unique: false });
 
  // Create an index to search customers by email. We want to ensure that
  // no two customers have the same email, so use a unique index.
  objectStore.createIndex("email", "email", { unique: true });
 
  // Store values in the newly created objectStore.
  for (var i in customerData) {
    objectStore.add(customerData[i]);
  }
}
*/

/*
// Code to create object stores and add data
(function(){
  $.indexedDB("databaseName", {
    "schema": {
      "1": function(versionTransaction){
        versionTransaction.createObjectStore("objectStore1");
      },
      "2": function(versionTransaction){
        versionTransaction.createObjectStore("objectStore2");
      }
    }
  }).transaction(["objectStore1", "objectStore2"]).then(function(){
    console.log("Transaction completed");
  }, function(){
    console.log("Transaction aborted");
  }, function(t){
    console.log("Transaction in progress");
    t.objectStore("objectStore1").add({
      "valueProp": "val",
      "anotherProp": 2
    }, 1).then(function(){
      console.log("Data added");
    }, function(){
      console.log("Error adding data");
    });
  });
})();
*/


// var req = $.indexedDB("databaseName");
// req.onsuccess = function(event){
	// console.log(event);
// }

/*
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
function openIndexedDB(version) {
	const dbName = "MyTestDatabase";
	var request = version > 0 ? indexedDB.open(dbName, version) : indexedDB.open(dbName);
	request.onsuccess = function(e){
		console.log('onsuccess: ' + e.target.result.version.toString() + ' ' + version);
	}
	request.onupgradeneeded = function(e){
		console.log('onupgradeneeded: ' + e.target.result.version.toString() + ' ' + version);
	}
}

var version = 1
openIndexedDB(version+1);
openIndexedDB(version+2);
openIndexedDB(version+3);
openIndexedDB(version+4);
*/
/*
// This is what our customer data looks like.
const customerData = [
  { ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" },
  { ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" }
];

request.onerror = function(event) {
  // Handle errors.
};
request.onupgradeneeded = function(event) {
  var db = event.target.result;
 
  // Create an objectStore to hold information about our customers. We're
  // going to use "ssn" as our key path because it's guaranteed to be
  // unique.
  var objectStore = db.createObjectStore("customers", { keyPath: "ssn" });
 
  // Create an index to search customers by name. We may have duplicates
  // so we can't use a unique index.
  objectStore.createIndex("name", "name", { unique: false });
 
  // Create an index to search customers by email. We want to ensure that
  // no two customers have the same email, so use a unique index.
  objectStore.createIndex("email", "email", { unique: true });
 
  // Store values in the newly created objectStore.
  for (var i in customerData) {
    objectStore.add(customerData[i]);
  }
}
*/

/*
//-----------------------------------

//local storage...
localStorage.setItem("a", JSON.stringify({
	"tables" : {
		"table1" : {
			rows : [{field1 : 1}]
		   ,fields : {field1 : {type : 'integer', isNullable : false}}
		}
	}
}));

console.log(localStorage.getItem("a"));
//console.log(localStorage);
*/



/*
console.log(HPI.isEqual(
	[{"LoginID" : 1, "StoreID" : 1, "LoginTime" : '23:59:59', "LoginSignature" : 'signature_test'}]
   ,[{"LoginID" : 1, "StoreID" : 1, "LoginTime" : '23:59:59', "LoginSignature" : 'signature_test'}]
));
*/

